package com.eatme.leetcode.lcof;

import com.eatme.core.queue.PriorityQueue;

import java.util.Arrays;
import java.util.Random;

/**
 * top k
 */
public class Solution40V2 {
    public int[] getLeastNumbers(int[] arr, int k) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        for (int i = 0; i < k; i++)
            priorityQueue.enqueue(arr[i]);
        for (int i = k; i < arr.length; i++)
            if (!priorityQueue.isEmpty() && arr[i] < priorityQueue.getFront()) {
                priorityQueue.dequeue();
                priorityQueue.enqueue(arr[i]);
            }
        int[] res = new int[k];
        for (int i = 0; i < k; i++) {
            res[i] = priorityQueue.dequeue();
        }
        return res;
    }
}